﻿namespace Devonline.Entity;

/// <summary>
/// 证书, 字符串表达式的默认实现
/// 服务器颁发给客户端的证书, 用于校验客户端身份
/// </summary>
[Table("certificate"), DisplayName("证书")]
public class Certificate : Certificate<string>, IEntitySet, IEntitySetWithCreate
{
    /// <summary>
    /// 用户个人信息
    /// </summary>
    public virtual Personal? Personal { get; set; }
    /// <summary>
    /// 客户端
    /// </summary>
    public virtual Client? Client { get; set; }
}

/// <summary>
/// 证书
/// 服务器颁发给客户端的证书, 用于校验客户端身份
/// </summary>
[Table("certificate"), DisplayName("证书")]
public class Certificate<TKey> : EntitySetWithCreate<TKey>, IEntitySet<TKey>, IEntitySetWithCreate<TKey> where TKey : IConvertible
{
    /// <summary>
    /// 用户个人信息
    /// </summary>
    [Column("personal_id"), DisplayName("个人信息编号"), MaxLength(36), Excel]
    public virtual TKey? PersonalId { get; set; }
    /// <summary>
    /// 所有者客户端编号
    /// </summary>
    [Column("client_id"), DisplayName("所有者客户端编号"), Required, MaxLength(36), Excel]
    public virtual TKey? ClientId { get; set; }
    /// <summary>
    /// 签名算法(证书非对称算法名称)
    /// </summary>
    [Column("signature_algorithm", TypeName = "VARCHAR(16)"), DisplayName("签名算法"), Required, DefaultValue(EncryptionAlgorithm.RSA), Excel]
    public virtual EncryptionAlgorithm SignatureAlgorithm { get; set; }
    /// <summary>
    /// 散列算法(证书散列算法名称)
    /// </summary>
    [Column("hash_algorithm", TypeName = "VARCHAR(16)"), DisplayName("散列算法"), Required, DefaultValue(HashAlgorithm.SHA256), Excel]
    public virtual HashAlgorithm HashAlgorithm { get; set; }
    /// <summary>
    /// 签名算法密钥长度, 默认 2048 位
    /// </summary>
    [Column("secret_length"), DisplayName("非对称密钥长度"), DefaultValue(2048), Excel]
    public virtual int SecretLength { get; set; }
    /// <summary>
    /// 有效期起
    /// </summary>
    [Column("start_time"), DisplayName("有效期起"), Excel]
    public virtual DateTime StartTime { get; set; }
    /// <summary>
    /// 有效期至
    /// </summary>
    [Column("end_time"), DisplayName("有效期至"), Excel]
    public virtual DateTime EndTime { get; set; }
    /// <summary>
    /// 证书公钥, 非对称算法公钥, 用于加密用户密钥, 保存为明文, 密钥长度 2048 位, 256 字节
    /// </summary>
    [Column("public_key"), DisplayName("证书公钥"), MaxLength(256), Excel]
    public virtual byte[]? PublicKey { get; set; }
    /// <summary>
    /// 证书内容
    /// </summary>
    [Column("content"), DisplayName("证书内容"), MaxLength(4096), Excel]
    public virtual byte[]? Content { get; set; }
}